package Stream;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.HashMap;

public class MapUpdate extends JInternalFrame {
	
	HashMap<String , MapData> map;
	String str;
	boolean findFlag;
	private JLabel lblNewLabel;
	private JTextField userId;
	private JLabel lblId;
	private JButton btnNewButton;
	private JLabel lblName;
	private JLabel lblPhone;
	private JTextField irum;
	private JTextField phone;
	private JButton btnNewButton_1;
	private JButton button;
	private JLabel status;

	/**
	 * Create the frame.
	 */
	public MapUpdate(HashMap<String, MapData> m) {
		super("Update",true, true, true, false);
		this.map = m;
		setBounds(100, 100, 313, 210);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getUserId());
		getContentPane().add(getLblId());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getLblName());
		getContentPane().add(getLblPhone());
		getContentPane().add(getIrum());
		getContentPane().add(getPhone());
		getContentPane().add(getBtnNewButton_1());
		getContentPane().add(getButton());
		getContentPane().add(getStatus());

	}

	public void setMap(HashMap m) {
		this.map = m;
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("--------------------------------------------------------");
			lblNewLabel.setBounds(12, 41, 410, 15);
		}
		return lblNewLabel;
	}
	private JTextField getUserId() {
		if (userId == null) {
			userId = new JTextField();
			userId.setBounds(60, 10, 116, 21);
			userId.setColumns(10);
		}
		return userId;
	}
	private JLabel getLblId() {
		if (lblId == null) {
			lblId = new JLabel("ID");
			lblId.setBounds(12, 13, 36, 15);
		}
		return lblId;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("조회");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					str = userId.getText();//userId칸의 문자열을 str저장
					findFlag = false;
					if(str.equals("")){
						status.setText("검색할 데이터를 입력하세요.");
						return;
					}
					MapData value = map.get(str);//str을 키로 사용해서 검색해서 가져온값을 value에 저장
					if(value==null){
						JOptionPane.showMessageDialog(null, "검색에 실패하였습니다.");
						return;
					}
					findFlag=true;//조회에 성공하면 true로 바뀜
					irum.setText(value.irum); 
					phone.setText(value.phone);
					status.setText(str+"이 검색되었습니다.");
					
				}
			});
			btnNewButton.setBounds(188, 9, 97, 23);
		}
		return btnNewButton;
	}
	private JLabel getLblName() {
		if (lblName == null) {
			lblName = new JLabel("NAME");
			lblName.setBounds(12, 66, 57, 15);
		}
		return lblName;
	}
	private JLabel getLblPhone() {
		if (lblPhone == null) {
			lblPhone = new JLabel("PHONE");
			lblPhone.setBounds(12, 91, 57, 15);
		}
		return lblPhone;
	}
	private JTextField getIrum() {
		if (irum == null) {
			irum = new JTextField();
			irum.setBounds(81, 63, 204, 21);
			irum.setColumns(10);
		}
		return irum;
	}
	private JTextField getPhone() {
		if (phone == null) {
			phone = new JTextField();
			phone.setBounds(81, 88, 204, 21);
			phone.setColumns(10);
		}
		return phone;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("수정");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (findFlag) {//조회가 성공햇을시에만 실행
						if (!str.equals(userId.getText())) {
							JOptionPane.showMessageDialog(null,"id는 변경하실수 없습니다.");
							return;
						}
						MapData upmap = new MapData(str, irum.getText(), phone.getText());//새로운 upmap 생성
						map.put(str, upmap);//upmap을 map에 저장(키값인 아이디가 동일하기때문에 기존값이 바뀜)

						userId.setText("");
						irum.setText("");
						phone.setText("");
						status.setText(str + "이 수정되었습니다.");
					}else{
						status.setText("수정이취소되었습니다.");
					}
					
				}
			});
			btnNewButton_1.setBounds(12, 116, 129, 23);
		}
		return btnNewButton_1;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("삭제");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
					if (findFlag) {
						int b = JOptionPane.showConfirmDialog(null,
								"str가 삭제됩니다.");
						if (b == JOptionPane.YES_OPTION) {
							map.remove(str);//str과 키값이 겹치는것을 찾아서 삭제
							userId.setText("");
							irum.setText("");
							phone.setText("");
							status.setText(str + "이 삭제 되었습니다.");
						} else {
							status.setText("삭제가 취소 되었습니다.");
						}
					} else{
						status.setText("데이터가 검색되지않았습니다.");
					}
				}
			});
			button.setBounds(156, 116, 129, 23);
		}
		return button;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("");
			status.setOpaque(true);
			status.setBackground(new Color(176, 224, 230));
			status.setBounds(12, 149, 273, 15);
		}
		return status;
	}
}
